//===============================================================================
// Microsoft patterns & practices
// Guidance Explorer Offline Client
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using GuidanceExplorer.Model.Conditions;
//using EnvDTE;

namespace GuidanceExplorer.Model
{
    /// <summary>
    /// Represents an item that exists in the file system.
    /// </summary>
    //[MenuItemAttribute(1, "context\\New Item", GuidanceExplorer.Commands.NewGuidanceItem, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
    //[MenuItemAttribute(5, "context\\New Item Template", GuidanceExplorer.Commands.NewGuidanceItem, typeof(ModelItemMenuItemIsEditableCondition), typeof(NewItemMenuItemProvider), Keys.None)]
    //[MenuItemAttribute(10, "context\\View Item...", "View Items...", GuidanceExplorer.Commands.ViewGuidanceItem, Keys.None)]
    //[MenuItemAttribute(20, "context\\Edit Item...", "Edit Items...", GuidanceExplorer.Commands.EditGuidanceItem, Keys.None)]
    //[MenuItemAttribute(30, "context\\Export Item as Xml...", "Export Items as Xml...", GuidanceExplorer.Commands.SaveItemAsXml, Keys.None)]
    //[MenuItemAttribute(40, "context\\Save Item As...", "Save Items As...", GuidanceExplorer.Commands.SaveItemAs, Keys.None)]
    //[MenuItemAttribute(50, "context\\Delete Item", "Delete Items", GuidanceExplorer.Commands.DeleteGuidanceItem, Keys.None)]
    //[MenuItemAttribute(58, "context\\-", "", Keys.None)]
    //[MenuItemAttribute(59, "context\\Add To View...", GuidanceExplorer.Commands.MoveGuidanceItemToView, Keys.None)]
    //[MenuItemAttribute(60, "context\\-", "", Keys.None)]
    //[MenuItemAttribute(70, "context\\Reset Sort Order", GuidanceExplorer.Commands.ResetSortOrder, Keys.None)]
    //[MenuItemAttribute(80, "context\\Reset Columns", GuidanceExplorer.Commands.ResetColumns, Keys.None)]
    //[MenuItemAttribute(90, "context\\Reset Filters", GuidanceExplorer.Commands.ResetFilters, Keys.None)]
    //[MenuItemAttribute(100, "context\\Column Chooser", GuidanceExplorer.Commands.ColumnChooser, Keys.None)]
    ////[MenuItemAttribute(110, "context\\-", "", Keys.None)]
    ////[MenuItemAttribute(120, "context\\Comment...", "NewComment", Keys.None)]
    //[MenuItemAttribute(130, "context\\-", "", typeof(WebSubscribedLibraryCondition), Keys.None)]
    //[MenuItemAttribute(140, "context\\Synchronize Library", Commands.Synchronize, typeof(WebSubscribedLibraryCondition), Keys.None)]
	
	[MenuItemAttribute(520, "context\\Export Item as Xml...", "Export Items as Xml...", GuidanceExplorer.Commands.SaveItemAsXml, typeof(NotGenericMenuItemCondition<MenuItemUnderSolutionNodeCondition>), Keys.None)]
	[MenuItemAttribute(525, "context\\-", "", typeof(NotGenericMenuItemCondition<MenuItemUnderSolutionNodeCondition>), Keys.None)]
    [MenuItemAttribute(530, "context\\Add To View...", GuidanceExplorer.Commands.MoveGuidanceItemToView, typeof(NotGenericMenuItemCondition<MenuItemUnderSolutionNodeCondition>), Keys.None)]
    [MenuItemAttribute(540, "context\\-", "", typeof(NotGenericMenuItemCondition<MenuItemUnderSolutionNodeCondition>), Keys.None)]
    [MenuItemAttribute(550, "context\\Edit", GuidanceExplorer.Commands.EditGuidanceItem, Keys.None)]
    [MenuItemAttribute(560, "context\\Delete", GuidanceExplorer.Commands.DeleteGuidanceItem, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
    //[MenuItemAttribute(570, "context\\Reset Filters", GuidanceExplorer.Commands.ResetFilters, Keys.None)]
    
    public class GuidanceItemFileReference : GuidanceItem
    {
        /// <summary>
        /// Factory.
        /// </summary>
        /// <returns></returns>
        public static new GuidanceItemFileReference New()
        {
            return new GuidanceItemFileReference();
        }


        /// <summary>
        /// The file name for the reference.
        /// </summary>
        private string _fileName;

        /// <summary>
        /// Default private constructor.
        /// </summary>
        protected GuidanceItemFileReference()
        {
            this.Title = String.Empty;
        }

        /// <summary>
        /// The file name for the reference.
        /// </summary>
        [Browsable(true)]
        [ReadOnly(true)]
        public string FileName
        {
            get 
            { 
                return _fileName;
            }
            set
            {
                _fileName = value;
                CustomData["filePath"] = value;
                //by adding first it saves a check to see if the item contains this attribute
                if (String.IsNullOrEmpty(value))
                {
                    CustomData.Remove("filePath");
                }
            }
        }

        [Browsable(true)]
        [ReadOnly(true)]
        public string FullFileName
        {
            get 
            { 
                if (Path.IsPathRooted(_fileName))
                {
                    return _fileName;
                }
                else if (_fileName.StartsWith(@"..\Libraries"))
                {
                    string fullFileName = Path.Combine(GuidanceExplorer.Services.ModelStorageService.LibraryFolder,
                        _fileName.Substring(@"..\Libraries".Length+1, _fileName.Length - @"..\Libraries".Length-1));
                    return new FileInfo(fullFileName).FullName;
                }
                else
                {
                    string fullFileName = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), _fileName);
                    return new FileInfo(fullFileName).FullName;
                }
            }
        }

        #region ICloneable Members

        /// <summary>
        /// Creates a new instance of the GuidanceItemFileReference with the same data.
        /// </summary>
        /// <returns></returns>
        public GuidanceItemFileReference Clone()
        {
            GuidanceItemFileReference gifr = GuidanceItemFileReference.New();

            gifr.CSSFile = this.CSSFile;
            gifr.FileName = FileName;
            gifr.GuidanceType = this.GuidanceType;
            gifr.Id = Guid.NewGuid().ToString();
            gifr.Title = this.Title;
            gifr.AllImages = this.AllImages;

            foreach (KeyValuePair<String, String> pair in this.CustomData)
            {
                string key = pair.Key;
                gifr.CustomData[key] = this.CustomData[key];
            }

            return gifr;
        }

        public override int GetHashCode()
        {
            if (string.IsNullOrEmpty(Id))
            {
                return _fileName.GetHashCode();
            }
            else
            {
                return Id.GetHashCode();
            }
        }

        #endregion
    }
}

